package com.south.base.utils;

import org.springframework.util.CollectionUtils;

import java.util.LinkedList;

/**
 * @author Administrator
 * @date 2019/8/28 16:22
 */
public class SortUtil {
    /**
     * 冒泡排序
     */
    public static void bubbleSort(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    swap(nums, i, j);
                }
            }
        }
    }

    /**
     * 插入排序
     *
     * @param nums
     */
    public static void insertSort(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        insertSort(nums, 1);
    }

    private static void insertSort(int[] nums, int interval) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        for (int i = 0; i < nums.length; i += interval) {
            int current = nums[i];
            for (int j = 0; j < i; j += interval) {
                if (nums[j] > current) {
                    System.arraycopy(nums, j, nums, j + 1, i - j);
                    nums[j] = current;
                    break;
                }
            }
        }
    }

    /**
     * 选择排序
     * 选择一个最小的值放到当前位置
     *
     * @param nums
     */
    public static void selectionSort(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            int minNumIndex = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[i]) {
                    minNumIndex = j;
                }
            }
            if (minNumIndex != i) {
                swap(nums, i, minNumIndex);
            }
        }
    }

    /**
     * 堆排序
     */
    public static void heapSort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            adjustHeap(nums, i, nums.length);
        }
        for (int i = 0; i < nums.length; i++) {
            swap(nums, 0, i);
            adjustHeap(nums, 0, i);
        }
    }

    /**
     * 调整最大堆
     *
     * @param arr
     * @param i
     * @param length
     */
    private static void adjustHeap(int[] arr, int i, int length) {
        //先取出当前元素i
        int temp = arr[i];
        //数组模拟堆结构时，数组下标为i的节点，两个子节点分别为2i+1，2(i+1)
        //从i结点的左子结点开始，也就是2i+1处开始
        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if (k + 1 < length && arr[k] < arr[k + 1]) {
                //如果左子结点小于右子结点，k指向右子结点
                k++;
            }
            if (arr[k] > temp) {
                //如果子节点大于父节点，将子节点值赋给父节点（不用进行交换）
                arr[i] = arr[k];
                i = k;
            } else {
                break;
            }
        }
        //将temp值放到最终的位置
        arr[i] = temp;
    }


    /**
     * 希尔排序
     */
    public static void shellSort(int[] nums) {
        //原始希尔排序间隔取法
        //Hibbard Math.pow(2, k) - 1
        //Sedgewick 9 * Math.pow(4, i) - 9 * Math.pow(2, i) + 1 或者是Math.pow(4, i) - 3 * Math.pow(2, i) + 1
        for (int i = nums.length / 2; i > 0; i /= 2) {
            insertSort(nums, i);
        }
    }

    /**
     * 归并排序
     *
     * @param nums
     */
    public static void mergeSort(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        int[] tmp = new int[nums.length];
        System.arraycopy(nums, 0, tmp, 0, nums.length);
        mergeSort(nums, tmp, 0, nums.length - 1);
    }

    private static void mergeSort(int[] nums, int[] tmp, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSort(nums, tmp, left, mid);
        mergeSort(nums, tmp, mid + 1, right);
        mergeSort(nums, tmp, left, mid, right);
    }

    private static void mergeSort(int[] arr, int[] temp, int left, int mid, int right) {
        int i = left, j = mid + 1, t = 0;
        while (i <= mid || j <= right) {
            if (i > mid || (j <= right && arr[i] > arr[j])) {
                temp[t++] = arr[j++];
            } else {
                temp[t++] = arr[i++];
            }
        }
        t = 0;
        //将temp中的元素全部拷贝到原数组中
        while (left <= right) {
            arr[left++] = temp[t++];
        }
    }

    /**
     * 快速排序
     *
     * @param nums
     */
    public static void quickSort(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
    }

    private static void quickSort(int[] nums, int start, int end) {
        if (nums == null || nums.length <= 1 || end - start < 1) {
            return;
        }
        // 选取主元，取头中尾得中位数
        if (nums[start] > nums[(start + end) / 2]) {
            swap(nums, start, (start + end) / 2);
        }
        if (nums[start] > nums[end]) {
            swap(nums, start, end);
        }
        if (nums[(start + end) / 2] > nums[end]) {
            swap(nums, (start + end) / 2, end);
        }
        swap(nums, (start + end) / 2, end - 1);
        // 根据主元分成2个子集
        int left = start;
        int right = end - 1;
        while (right > left) {
            for (int i = left; i < end - 1; i++) {
                if (nums[i] > nums[end - 1]) {
                    left = i;
                    break;
                }
            }
            for (int i = right; i >= 0; i--) {
                if (nums[i] < nums[end - 1]) {
                    right = i;
                    break;
                }
            }
            if (right < left) {
                swap(nums, left, end - 1);
            } else {
                swap(nums, left, right);
            }
        }
        quickSort(nums, start, left - 1);
        quickSort(nums, left + 1, end);
    }

    /**
     * 桶排序
     *
     * @param nums
     */
    public static void bucketSort(int[] nums) {
        int max = 0;
        for (int value : nums) {
            if (max < value) {
                max = value;
            }
        }
        int[] bucket = new int[max];
        for (int num : nums) {
            bucket[num - 1] += 1;
        }
        int j = 0;
        for (int i = 0; i < bucket.length; i++) {
            for (int k = 0; k < bucket[i]; k++) {
                nums[j++] = i + 1;
            }
        }
    }

    /**
     * 基数排序
     */
    public static void radixSort(int[] nums) {
        //代表位数对应的数：1,10,100...
        int n = 1;
        LinkedList<Integer>[] bucket1 = new LinkedList[10];
        //将数组array里的每个数字放在相应的桶里
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int num : nums) {
                int digit = (num / n) % 10;
                if (bucket1[digit] == null) {
                    bucket1[digit] = new LinkedList<>();
                }
                bucket1[digit].addLast(num);
            }
            //将前一个循环生成的桶里的数据覆盖到原数组中用于保存这一位的排序结果
            for (int j = 0; j < nums.length; j++) {
                for (int i = 0; i < bucket1.length;) {
                    //这个桶里有数据，从上到下遍历这个桶并将数据保存到原数组中
                    if (!CollectionUtils.isEmpty(bucket1[i])) {
                        if (i > 0) {
                            flag = true;
                        }
                        Integer num = bucket1[i].pollFirst();
                        if (num == null) {
                            i++;
                            continue;
                        }
                        nums[j++] = num;
                    } else {
                        i++;
                    }
                }
            }
            n *= 10;
        }
    }

    /**
     * 交换数组元素
     *
     * @param nums
     * @param i
     * @param j
     */
    public static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
